import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../components/home.vue'
import MyUser from '../views/MyUser.vue'
import Login from '../views/Login.vue'
import NotFound from '../views/404.vue'
import About from '../views/About.vue'
import Register from '@/views/zhuce.vue'
import shiwureliangchaxun from '../views/shiwureliangchaxun.vue'
import yingyangchengfenfenxi from '../views/yingyangchengfenfenxi.vue'
import yinsijihuazhiding from '../views/yinsijihuazhiding.vue'
import shiputuijian from '../views/shiputuijian.vue'
import yinshijinjitixing from '../views/yinshijinjitixing.vue'
import jiankangyinshixiaotieshi from '../views/jiankangyinshixiaotieshi.vue'
import tizhongguanli from '../views/tizhongguanli.vue'
import yundongjianyi from '../views/yundongjianyi.vue'
import yinshijilu from '../views/yinshijilu.vue'
import FoodDetailVue from '@/components/FoodDetail.vue'
import shipudetailVue from '@/components/shipudetail.vue'


const routes = [
  {
    path: '/',
    name: 'Home',
    component: HomeView
  },
  {
    path: '/shipudetailVue',
    name: 'shipudetailVue',
    component: shipudetailVue
  },
  {
    path: '/shiwureliangchaxun',
    name: 'shiwureliangchaxun',
    component: shiwureliangchaxun, 
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/shiwureliangchaxun/:id',
    name: 'FoodDetailVue',
    component: FoodDetailVue, 
    props: true
  },
  {
    path: '/my',
    name: 'my',
    component: MyUser,
    meta: { 
      requiresAuth: true
    }
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/about',
    name: 'about',
    component: About
  },
  {
    path: '/yingyangchengfenfenxi',
    name: 'yingyangchengfenfenxi',
    component: yingyangchengfenfenxi,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/yinsijihuazhiding',
    name: 'yinsijihuazhiding',
    component: yinsijihuazhiding,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/shiputuijian',
    name: 'shiputuijian',
    component: shiputuijian,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/yinshijinjitixing',
    name: 'yinshijinjitixing',
    component: yinshijinjitixing,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/jiankangyinshixiaotieshi',
    name: 'jiankangyinshixiaotieshi',
    component: jiankangyinshixiaotieshi
  },
  {
    path: '/tizhongguanli',
    name: 'tizhongguanli',
    component: tizhongguanli,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/yundongjianyi',
    name: 'yundongjianyi',
    component: yundongjianyi,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/yinshijilu',
    name: 'yinshijilu',
    component: yinshijilu,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'not-found',
    component: NotFound
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 改进的路由守卫
router.beforeEach((to, from, next) => {
  // 从localStorage获取登录状态
  const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true'
  const userName = localStorage.getItem('userName') || ''

  // 检查路由是否需要认证
  if (to.matched.some(record => record.meta.requiresAuth) && !isLoggedIn) {
    next({ name: 'login' })
    return
  }

  // 检查是否访问的是登录页但已登录
  if (to.name === 'login' && isLoggedIn) {
    next({ name: 'Home' })
    return
  }

  // 其他情况正常放行
  next()
})

export default router